#Based on Example 5.2.rb
$: << File.expand_path(File.dirname(__FILE__) + "/../lib")
sourcepath =  File.expand_path("/Users/matthias/Documents/ETH/tecdoc/woehrlem/paper/woehrlem2008_sensys_poster/trunk/src/tossim")

require 'rupeas'

Rupeas.new do


Type :senddone do
 with :name =>:seqNo, :fieldtype=> :periodic, :range => 0..255, :notification => :error
 with :name =>:origin, :fieldtype => :integer, :range => 0..100,:notification => :error
 with :name =>:destination, :fieldtype => :integer, :range => 0..100, :notification => :warning
 with :name =>:nodeid,:fieldtype=> :integer, :range => 0..100,:notification => :error
 with :name =>:time, :fieldtype => :float, :notification => :warning 
end

Type :receive do
 with :name =>:seqNo, :fieldtype=> :periodic, :range => 0..255, :notification => :error
 with :name =>:origin, :fieldtype => :integer, :range => 0..100,:notification => :error
 with :name =>:destination, :fieldtype => :integer, :range => 0..100, :notification => :warning
 with :name =>:nodeid,:fieldtype=> :integer, :range => 0..100,:notification => :error
 with :name =>:time, :fieldtype => :float, :notification => :warning 
end

all = loadlog(sourcepath + '/tiny.log')


#Note : send is a ruby keyword for sending a method to an object - need to prohibit or provide workaround
#merge is also problematic since it is used by set
routestart = all.transform do |sending|
  constraint sending[:origin] == sending[:nodeid]       # partition
  constraint sending[:type] == :senddone                # filter
  merging :type => :route,:nodeid => sending[:nodeid],:seqNo => sending[:seqNo],:origin => sending[:origin]
end


routes = routestart.transform(:iterative) do |sending, receive|
  constraint sending[:origin] == receive[:origin]        # partition (1)
  constraint sending[:seqNo] == receive[:seqNo]             # partition (2)
  select sending[:nodeid] == receive[:nodeid] and receive[:type] == :route and sending[:type] == :senddone
  select sending[:dest] == receive[:nodeid] and sending[:type] == :route and sending[:type] == :receive
  merging :type => :transmission, :nodeid => receive[:nodeid], :seqNo => sending[:seqNo], :origin => sending[:origin]
end
#    
#routestart = all.transform do |send, receive|
#  constraint send[:origin] == receive[:origin]
#  constraint send[:dest] = receive[:nodeid] and send[:seqNo] == receive[:seqNo]
#  merge :type => :send_success, :origin => send[:origin],:nodeid   => send[:nodeid], :seqNo   => send[:seqNo]       
#  merge :type => :receive_success, :origin => receive[:origin], :nodeid   => receive[:nodeid], :seqNo   => send[:seqNo]    
#end

end